package com.woniu.ly.manageassembly.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.SerializerMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class RabbitConfig {

    public static final String HOT_TRAVEL_EXCHANGE = "hot.travel.exchange";
    public static final String HOT_TRAVEL_QUEUE = "hot.travel.queue";
    public static final String HOT_TRAVEL_ROUTING_KEY = "hot.travel";

    @Bean
    public TopicExchange hotTravelTopicExchange(){
        return new TopicExchange(HOT_TRAVEL_EXCHANGE);
    }

    @Bean
    public Queue hotTravelQueue(){
        return new Queue(HOT_TRAVEL_QUEUE);
    }

    @Bean
    public Binding hotTravelBinding(){
        return BindingBuilder.bind(hotTravelQueue()).to(hotTravelTopicExchange()).with(HOT_TRAVEL_ROUTING_KEY);
    }

    @Bean
    public Queue cardTimeQueue(){
        return new Queue("card.time.queue");
    }
    @Bean
    public TopicExchange cardTimeExchange(){
        return new TopicExchange("card.time.exchange");
    }
    @Bean
    public Binding cardTimeBinding(){
        return BindingBuilder.bind(cardTimeQueue()).to(cardTimeExchange()).with("card.time.key");
    }
    @Bean
    @Scope("prototype")
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMandatory(true);
        template.setMessageConverter(new SerializerMessageConverter());
        return template;
    }
    /*
    * 退款服务
    * */
    @Bean
    public TopicExchange refundTopicExchange(){
        return new TopicExchange("refund.money.exchange");
    }
    @Bean
    public Queue refundQueue(){
        return new Queue("refund.money.queue");
    }
    @Bean
    public Binding refundBinding(){
        return BindingBuilder.bind(refundQueue()).to(refundTopicExchange()).with("refund.money.key");
    }
}
